#ifndef VIDEOSETTINGS_H
#define VIDEOSETTINGS_H

#include <QObject>
#include <QSettings>
#include <QSize>
#include <QString>
#include <QVariant>
#include <QQmlEngine>


class VideoSettings : public QObject
{
    Q_OBJECT

    // 视频基本设置
    Q_PROPERTY(QString uri READ uri WRITE setUri NOTIFY uriChanged)
    Q_PROPERTY(int width READ width WRITE setWidth NOTIFY widthChanged)
    Q_PROPERTY(int height READ height WRITE setHeight NOTIFY heightChanged)
    Q_PROPERTY(QSize resolution READ resolution WRITE setResolution NOTIFY resolutionChanged)
    Q_PROPERTY(int frameRate READ frameRate WRITE setFrameRate NOTIFY frameRateChanged)
    Q_PROPERTY(int bitRate READ bitRate WRITE setBitRate NOTIFY bitRateChanged)
    Q_PROPERTY(unsigned timeout READ timeout WRITE setTimeout NOTIFY timeoutChanged)
    Q_PROPERTY(int buffer READ buffer WRITE setBuffer NOTIFY bufferChanged)
    Q_PROPERTY(int cacheTime READ cacheTime WRITE setCacheTime NOTIFY cacheTimeChanged)

public:
    enum MEDIA_TYPE {
        TYPE_TEST,
        TYPE_MP4,
        TYPE_RTSP,
    };
    Q_ENUM(MEDIA_TYPE)

private:
    explicit VideoSettings(QObject *parent = nullptr);

public:
    static QObject* GetInstance(QQmlEngine *, QJSEngine *);
    virtual ~VideoSettings();
    
    // 视频基本设置 - Getters
    QString uri() const { return m_uri; }
    int width() const { return m_width; }
    int height() const { return m_height; }
    QSize resolution() const { return QSize(m_width, m_height); }
    int frameRate() const { return m_frameRate; }
    int bitRate() const { return m_bitRate; }
    unsigned timeout() const { return m_timeout; }
    int buffer() const { return m_buffer; }
    int cacheTime() const { return m_cacheTime; }
    
public slots:
    // 视频基本设置 - Setters
    void setUri(const QString &uri);
    void setWidth(int width);
    void setHeight(int height);
    void setResolution(const QSize &resolution);
    void setFrameRate(int frameRate);
    void setBitRate(int bitRate);
    void setTimeout(unsigned timeout);
    void setBuffer(int buffer);
    void setCacheTime(int cacheTime);
    
    // 配置管理方法
    Q_INVOKABLE void loadSettings();
    Q_INVOKABLE void saveSettings();
    Q_INVOKABLE void resetToDefaults();
    Q_INVOKABLE QVariant getSetting(const QString &key, const QVariant &defaultValue = QVariant()) const;
    Q_INVOKABLE void setSetting(const QString &key, const QVariant &value);
    
    // 便捷方法
    Q_INVOKABLE void setResolution(int width, int height);
    Q_INVOKABLE QString getResolutionString() const;
    Q_INVOKABLE QStringList getCommonResolutions() const;
    
signals:
    // 视频基本设置信号
    void uriChanged(const QString &uri);
    void widthChanged(int width);
    void heightChanged(int height);
    void resolutionChanged(const QSize &resolution);
    void frameRateChanged(int frameRate);
    void bitRateChanged(int bitRate);
    void timeoutChanged(unsigned timeout);
    void bufferChanged(int buffer);
    void cacheTimeChanged(int cacheTime);
    
    // 通用信号
    void settingsChanged();
    void settingsLoaded();
    void settingsSaved();
    
private:
    void initializeDefaults();
    
    // 视频基本设置
    QString     m_uri;
    MEDIA_TYPE  m_type;
    int m_width;
    int m_height;
    int m_frameRate;
    int m_bitRate;
    unsigned m_timeout;
    int m_buffer;
    int m_cacheTime;
    
    // 配置存储
    QSettings *m_settings;
};

#endif // VIDEOSETTINGS_H
